package cn.dao.imp;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.dao.GoodsDao;
import cn.entity.Goods;

public class GoodsDaoImp extends HibernateDaoSupport implements GoodsDao {

	/*
	 * 插入一条goods数据
	 * 
	 * @see cn.dao.GoodsDao#insert(cn.entity.Goods)
	 */
	@Override
	public void insertOrUpdate(Goods goods) {
		getHibernateTemplate().saveOrUpdate(goods);
	}

	/*
	 * 按照实体类删除
	 * 
	 * @see cn.dao.GoodsDao#delete(cn.entity.Goods)
	 */
	@Override
	public void delete(Goods goods) {
		goods = (Goods) getHibernateTemplate().get(Goods.class, goods.getId());
		goods.setIsDelete(1);
		getHibernateTemplate().update(goods);
	}

	/*
	 * 按照id删除
	 * 
	 * @see cn.dao.GoodsDao#delete(java.lang.Integer)
	 */
	@Override
	public void delete(Integer id) {
		Goods goods = new Goods();
		goods.setId(id);
		getHibernateTemplate().delete(goods);

	}

	/*
	 * 更新一条goods信息
	 * 
	 * @see cn.dao.GoodsDao#update(cn.entity.Goods)
	 */
	@Override
	public void update(Goods goods) {
		getHibernateTemplate().update(goods);

	}

	/*
	 * 获得全部在售goods对象
	 * 
	 * @see cn.dao.GoodsDao#getAll()
	 */
	@Override
	public List getAll() {
		return getHibernateTemplate().find("from Goods where isDelete=0");
	}

	/*
	 * 获得全部goods对象
	 * @see cn.dao.GoodsDao#getManage()
	 */
	@Override
	public List<Goods> getManage() {
		return getHibernateTemplate().find("from Goods");
	}
	
	/*
	 * 获预留的条件查询位置
	 * 
	 * @see cn.dao.GoodsDao#query(cn.entity.Goods)
	 */
	@Override
	public List query(Goods goods) {
		goods.setIsDelete(0);
		return getHibernateTemplate().findByExample(goods);
	}

	@Override
	public List select(Goods goods) {
		return getHibernateTemplate().find(
				"from Goods g where g.isDelete=0 and g.goodsname like ?",
				"%" + goods.getGoodsname() + "%");
	}
	
	@Override
	public List getGoodsBySeller(Integer id) {
		return getHibernateTemplate().find("from Goods g where g.user ="+id);
	}

	/*
	 * 根据id获取商品
	 */

	@Override
	public Goods getGoodsById(Integer id) {
		return getHibernateTemplate().get(Goods.class, id);
	}
}
